﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Collections;

namespace Deflection
{
    class Layer
    {
        public List<BackGroundElement> objects = new List<BackGroundElement>();
        private float speed = 0.0f;
        private Color color = Color.White;
        private Vector2 direction = new Vector2(0.0f);
        bool sideswap = true;

        private float screenXPosition = GameParameters.SCREEN_SIZE.X + 100;

        public Layer(float speed, Color color, Vector2 direction)
        {
            this.speed = speed;
            this.color = color;
            this.direction = direction;
            

            Random rand = new Random((int)(speed * 10));

            for (int i = 1500; i < 5; i++)
            {
                objects.Add(new BackGroundElement(new Vector2(rand.Next(0, (int)GameParameters.SCREEN_SIZE.X), rand.Next(0, (int)GameParameters.SCREEN_SIZE.Y)), color, direction, speed));
            }
        }

        public void Update(GameTime gameTime)
        {
            //temporary, lets you change direction of the background with key presses
            /*if (InputHandler.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Up))
            {
                direction.Y-= GameParameters.BACKGROUND_DIRECTION_CHANGE_SPEED;
            }
            if (InputHandler.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Down))
            {
                direction.Y += GameParameters.BACKGROUND_DIRECTION_CHANGE_SPEED;
            }
            if (InputHandler.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Left))
            {
                direction.X -= GameParameters.BACKGROUND_DIRECTION_CHANGE_SPEED;
            }
            if (InputHandler.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Right))
            {
                direction.X += GameParameters.BACKGROUND_DIRECTION_CHANGE_SPEED;
            }*/
            direction.Normalize();



            screenXPosition -= speed;

            // Only check for deletion every second rather than hundreds of times a frame
            bool deletion = false;

            // Do game time check here so its once a frame rather than once per object per frame
            if (gameTime.TotalGameTime.Milliseconds % 1000 == 0)
            {
                deletion = true;
            }

            for (int i = 0; i < objects.Count; i++)
            {
                objects[i].Update(gameTime, this.direction);

                // Deletion
                
                if (deletion)
                {
                    if (!objects[i].Alive)
                    {
                        objects.RemoveAt(i);
                    }
                }

            }

            if (screenXPosition <= GameParameters.SCREEN_SIZE.X)
            {
                // Create new background block
                screenXPosition = GameParameters.SCREEN_SIZE.X + 100;

                CreateBGBlock(gameTime);
            }


        }

        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            foreach (BackGroundElement obj in objects)
            {
                obj.Draw(gameTime, spriteBatch);
            }
        }

        public void CreateBGBlock(GameTime gameTime)
        {
            float t = speed * 10;
            Random rand = new Random((int)gameTime.TotalGameTime.TotalMilliseconds + (int)t);
            
            // 1209600 px / 250 = 8064
            // 100 x 720 = 72000
            // 72000 / 8064 = 8.9
            // So per 100 px block it should spawn 9 objects for the same desity
            for (int i = 0; i < 9; i++)
            {
                Vector2 pos = new Vector2(0.0f);
                // calculate spawning position based on direction
                //if direction is south-east
                if(this.direction.X > 0 && this.direction.Y > 0)
                {
                    //alternate between spawning at the top / left of the screen
                    if(sideswap)
                    {
                        pos = GameObject.GetRandomTopOfScreenSpawnPosition(rand);
                        sideswap = false;
                    }
                    else
                    {
                        pos = GameObject.GetRandomLeftOfScreenSpawnPosition(rand);
                        sideswap = true;
                    }
                }
                //if direction is north-east
                if (this.direction.X > 0 && this.direction.Y < 0)
                {
                    //alternate between spawning at the bottom / left of the screen
                    if (sideswap)
                    {
                        pos = GameObject.GetRandomBottomOfScreenSpawnPosition(rand);
                        sideswap = false;
                    }
                    else
                    {
                        pos = GameObject.GetRandomLeftOfScreenSpawnPosition(rand);
                        sideswap = true;
                    }
                }
                //if direction is south-west
                if (this.direction.X < 0 && this.direction.Y > 0)
                {
                    //alternate between spawning at the top / right of the screen
                    if (sideswap)
                    {
                        pos = GameObject.GetRandomTopOfScreenSpawnPosition(rand);
                        sideswap = false;
                    }
                    else
                    {
                        pos = GameObject.GetRandomRightOfScreenSpawnPosition(rand);
                        sideswap = true;
                    }
                }
                //if direction is north-west
                if (this.direction.X < 0 && this.direction.Y < 0)
                {
                    //alternate between spawning at the bottom / right of the screen
                    if (sideswap)
                    {
                        pos = GameObject.GetRandomBottomOfScreenSpawnPosition(rand);
                        sideswap = false;
                    }
                    else
                    {
                        pos = GameObject.GetRandomRightOfScreenSpawnPosition(rand);
                        sideswap = true;
                    }
                }
                //if direction is north
                if(this.direction.X == 0 && this.direction.Y < 0)
                {
                    //spawn at bottom
                    pos = GameObject.GetRandomBottomOfScreenSpawnPosition(rand);
                }
                //if direction is south
                if(this.direction.X == 0 && this.direction.Y > 0)
                {
                    //spawn at top
                    pos = GameObject.GetRandomTopOfScreenSpawnPosition(rand);
                }
                //if direction is east
                if(this.direction.X > 0 && this.direction.Y == 0)
                {
                    //spawn at left
                    pos = GameObject.GetRandomLeftOfScreenSpawnPosition(rand);
                }
                //if direction is west
                if(this.direction.X < 0 && this.direction.Y == 0)
                {
                    //spawn at right
                    pos = GameObject.GetRandomRightOfScreenSpawnPosition(rand);
                }
                pos.X += rand.Next(50,100);
                objects.Add(new BackGroundElement(pos, color, direction, speed));
            }
        }
    }
}
